package com.nnddkj.dsbridgejsdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.nnddkj.dsbridgejsdemo.myinterface.JsCallback;
import com.nnddkj.dsbridgejsdemo.util.JsApi;

import wendu.dsbridge.CompletionHandler;
import wendu.dsbridge.DWebView;
import wendu.dsbridge.OnReturnValue;

public class MainActivity extends AppCompatActivity implements JsCallback{
    private Button btn_sendJS;
    private DWebView dwebView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         dwebView= (DWebView) findViewById(R.id.dwebview);
        btn_sendJS=findViewById(R.id.btn_sendJS);
        dwebView.loadUrl("file:///android_asset/jstest/index.html");
//        dwebView.loadUrl("http://192.168.1.2:8080/pick/api/index.html");

        dwebView.addJavascriptObject(new JsApi(this), null);
        //打开调试模式
        DWebView.setWebContentsDebuggingEnabled(true);
//        dwebView. evaluateJavascript("");

        //原生控件点击操作  发送数据给JS
        btn_sendJS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在Java中调用Javascript API,原生传递值给js 可传json 例如 new Object[]{new Gson().toJson(data)}
                //第一个参数是约定注册的名称，与js接受处一致
                //第二个参数是要传递的值
                //第三个参数是接受js返回的回调，可用于js接受成功后，再去通知原生。
                String msg="欢迎使用",name="Android";
                dwebView.callHandler("nativeToJs",new Object[]{name,msg},new OnReturnValue<String>(){
                    @Override
                    public void onValue(String retValue) {
                        Log.d("jsbridge","call succeed,return value is "+retValue);
                    }

                });
            }
        });


    }


    @Override
    public void testSyn(Object params) {
        Log.i("dsfsdjfsfdsfsifsifs",params+"  testSyn");
        Toast.makeText(MainActivity.this,"回调数据是 ："+params+"  testSyn",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void testAsyn(Object msg, CompletionHandler handler) {
        Log.i("dsfsdjfsfdsfsifsifs",msg+"  testAsyn");
        Toast.makeText(MainActivity.this,"回调数据是 ："+msg+"  testAsyn",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void jsToNative(Object msg, CompletionHandler handler) {
        Log.i("dsfsdjfsfdsfsifsifs",msg+"  jsToNative");
        Toast.makeText(MainActivity.this,"回调数据是 ："+msg+"  jsToNative",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void getWenxin(Object msg, CompletionHandler<String> handler) {
        Log.i("dsfsdjfsfdsfsifsifs",msg+"  getWenxin");
        Toast.makeText(MainActivity.this,"回调数据是 ："+msg+"  getWenxin",Toast.LENGTH_SHORT).show();
    }
}
